package com.xinzhidi.auth.controller;

import cn.binarywang.wx.miniapp.api.WxMaQrcodeService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.executor.QrcodeBytesRequestExecutor;
import com.xinzhidi.auth.form.MyWxaCodeUnlimit;
import com.xinzhidi.common.core.domain.R;
import com.xinzhidi.common.core.web.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@Api(tags = "小程序操作")
@Slf4j
@RestController
@RequestMapping(value = "/wechatMa")
public class WechatMaController {

    @Autowired
    private WxMaService wxMaService;

    @Value("${backUrl:}")
    private String backUrl;


    /**
     * 小程序-获取登录二维码
     *
     * @return 登录二维码
     */
    @ApiOperation(value = "登录二维码", response = AjaxResult.class)
    @GetMapping("/getQrCode")
    public R<Object> getQrCode(@RequestParam("scene")  String scene) {
//        String sceneStr = MyStringUtil.getRandomString(8);
        log.info("登录二维码：{}",scene);
        try {
            MyWxaCodeUnlimit wxaCodeUnlimit = new MyWxaCodeUnlimit();
            wxaCodeUnlimit.setScene(scene);
            wxaCodeUnlimit.setPage("pages/msg/msg");
            wxaCodeUnlimit.setWidth(430);
            wxaCodeUnlimit.setAutoColor(true);
            wxaCodeUnlimit.setLineColor(null);
            wxaCodeUnlimit.setHyaline(false);
            wxaCodeUnlimit.setCheck_path(false);
            byte[] qrCodeFile = this.wxMaService.execute(new QrcodeBytesRequestExecutor(this.wxMaService.getRequestHttp()),
                    WxMaQrcodeService.GET_WXACODE_UNLIMIT_URL,
                    wxaCodeUnlimit);
//            byte[] qrCodeFile = wxMaService.getQrcodeService().createWxaCodeUnlimitBytes(sceneStr, "pages/login/login",430,true, null,false);
            return R.ok(qrCodeFile);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
    }

}
